import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';

/// 视频框架
/// 注意：video_player 插件在 iOS 模拟器上不能使用，必须要在 iOS 真机上进行测试。
/// Android 配置
// 在 AndroidManifest.xml 文件中的 <application> 配置项下加入如下权限。 AndroidManifest.xml 文件的路径是 <project root>/android/app/src/main/AndroidManifest.xml
//
// content_copy
// <manifest xmlns:android="http://schemas.android.com/apk/res/android">
//     <application ...>
//
//     </application>
//
//     <uses-permission android:name="android.permission.INTERNET"/>
// </manifest>
/// iOS 配置
// 针对 iOS，你需要在 <project root>/ios/Runner/Info.plist 路径下的 Info.plist 文件中加入如下配置。
//
// content_copy
// <key>NSAppTransportSecurity</key>
// <dict>
//   <key>NSAllowsArbitraryLoads</key>
//   <true/>
// </dict>
class VideoPlayerFrame extends StatefulWidget {
  const VideoPlayerFrame({Key? key}) : super(key: key);

  @override
  _VideoPlayerFrameState createState() => _VideoPlayerFrameState();
}

class _VideoPlayerFrameState extends State<VideoPlayerFrame> {
  late VideoPlayerController _videoPlayerController;
  late Future<void> _initializeVideoPlayerFuture;

  @override
  void initState() {
    _videoPlayerController = VideoPlayerController.asset(
        "https://flutter.github.io/img-for-api-docs/img/videos/butterfly.mp4");
    _initializeVideoPlayerFuture = _videoPlayerController.initialize();
    _videoPlayerController.setLooping(true);

    super.initState();
  }

  @override
  void dispose() {
    _videoPlayerController.dispose();

    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text("VideoPlayer")),
      floatingActionButton: FloatingActionButton(
          child: Icon(_videoPlayerController.value.isPlaying
              ? Icons.pause
              : Icons.play_arrow),
          onPressed: () {
            setState(() {
              if (_videoPlayerController.value.isPlaying) {
                _videoPlayerController.pause();
              } else {
                _videoPlayerController.play();
              }
            });
          }),
      body: FutureBuilder(
        future: _initializeVideoPlayerFuture,
        builder: (context, snapshot) {
          if (snapshot.connectionState == ConnectionState.done) {
            return AspectRatio(
              aspectRatio: _videoPlayerController.value.aspectRatio,
              child: VideoPlayer(_videoPlayerController),
            );
          } else {
            return const Center(
              child: CircularProgressIndicator(),
            );
          }
        },
      ),
    );
  }
}
